package com.rwx.myjetpack.player

import com.rwx.myjetpack.domain.Music
import com.rwx.myjetpack.lifecycle.AbsLifecycle
import com.rwx.myjetpack.lifecycle.ILifecycle
import com.rwx.myjetpack.lifecycle.LifeState

/**
 * Created by LH on 2023-05-25.
 * Time: 2023-05-25  15:15
 */
class PlayerPresenter private constructor():AbsLifecycle(){

    enum class PlayState{
        NONE,PLAYING,PAUSE,LOADING
    }

    val playerModel by lazy { PlayerModel() }

    companion object{
        val instance by lazy {
            PlayerPresenter()
        }
    }

    val player by lazy { MusicPlayer() }



    var currentMusic =DataListenerContainer<Music>()

    var currentPlayState=DataListenerContainer<PlayState>()



    //private val callbacklist= arrayListOf<IPlayerCallback>()



//    fun registerCallback(callback:IPlayerCallback){
//        if(!callbacklist.contains(callback)){
//            callbacklist.add(callback)
//        }
//
//    }
//
//    fun unRegisterCallBack(callback:IPlayerCallback){
//        callbacklist.remove(callback)
//    }

    fun doPlayOrPause() {

        if (currentMusic.value==null) {
            currentMusic.value=playerModel.getSongById("XXX")
        }

        player.play(currentMusic.value)

//        dispatchTitleChange("当前播放歌曲标题")
//        dispatchCoverChange("当前播放歌曲封面")
        println("currentPlayState  ${currentPlayState.value}")
        if(currentPlayState.value!=PlayState.PLAYING){
            //播放音乐
            //dispatchPlayingState()
            currentPlayState.value=PlayState.PLAYING
        }else{
            //dispatchPauseState()
            currentPlayState.value=PlayState.PAUSE
        }

    }


//    private fun dispatchPauseState() {
//        callbacklist.forEach {
//            it.onPlayPause()
//        }
//
//    }
//
//    private fun dispatchPlayingState() {
//        callbacklist.forEach {
//            it.onPlaying()
//        }
//
//    }

    fun doPlayNext() {

        currentMusic.value=playerModel.getSongById("YYY")


        //拿到下一首
//        dispatchTitleChange("切换下一首,标题变化")
//        dispatchCoverChange("切换下一首,封面变化")
        //设置给播放器
        //等待播放回调通知
        currentPlayState.value=PlayState.PLAYING


    }

//    private fun dispatchCoverChange(cover: String) {
//
//        callbacklist.forEach {
//            it.onCoverChange(cover)
//        }
//
//    }
//
//    private fun dispatchTitleChange(title:String) {
//
//        callbacklist.forEach {
//            it.onTitleChange(title)
//        }
//
//    }

    fun doPlayPre() {
        currentMusic.value=playerModel.getSongById("ZZZ")


//        dispatchTitleChange("切换上一首,标题变化")
//        dispatchCoverChange("切换上一首,封面变化")

        currentPlayState.value=PlayState.PLAYING

    }

    override fun onCreate() {

    }

    override fun onStart() {
        println("检测~~~检测GPS")
    }

    override fun onResume() {

    }

    override fun onPause() {

    }

    override fun onStope() {
        println("检测~~~停止检测GPS")
    }

    override fun onDestory() {

    }

    override fun onViewLifeStateChange(state: LifeState) {
        println("current state $state")

    }


}